package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.jc.football.newJz.data.QQListView;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by fanxs11 on 16/4/26.
 */
public class BeiDan_BQC_Adapter extends BeiDanAdapter{

    public BeiDan_BQC_Adapter(Context context, List<BeiDanBean> infos, QQListView listView, OnBeiDanCheckBallListener listener) {
        super(context, infos, listView, listener);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder childViewHolder;
        final BeiDanChildBean info = infos.get(groupPosition).childBeen.get(childPosition);
//
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.beidan_list_child_bqc, null);
            childViewHolder.matchNum_tv = convertView.findViewById(R.id.beidan_list_matchNum_tv);
            childViewHolder.matchName_tv = convertView.findViewById(R.id.beidan_list_matchName_tv);
            childViewHolder.endSaleTime_tv = convertView.findViewById(R.id.beidan_list_endSaleTime_tv);
            childViewHolder.home_tv = convertView.findViewById(R.id.beidan_list_home);
            childViewHolder.away_tv = convertView.findViewById(R.id.beidan_list_away);
            childViewHolder.bqc_ss = convertView.findViewById(R.id.beidan_list_bqc_ss);
            childViewHolder.bqc_sp = convertView.findViewById(R.id.beidan_list_bqc_sp);
            childViewHolder.bqc_sf = convertView.findViewById(R.id.beidan_list_bqc_sf);
            childViewHolder.bqc_ps = convertView.findViewById(R.id.beidan_list_bqc_ps);
            childViewHolder.bqc_pp = convertView.findViewById(R.id.beidan_list_bqc_pp);
            childViewHolder.bqc_pf = convertView.findViewById(R.id.beidan_list_bqc_pf);
            childViewHolder.bqc_fs = convertView.findViewById(R.id.beidan_list_bqc_fs);
            childViewHolder.bqc_fp = convertView.findViewById(R.id.beidan_list_bqc_fp);
            childViewHolder.bqc_ff = convertView.findViewById(R.id.beidan_list_bqc_ff);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.matchNum_tv.setText(info.getMatchNum()+"");
        childViewHolder.matchName_tv.setText(info.getMatchName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String endTimeStr = simpleDateFormat.format(new Date(info.getEndSaleTime()));
        childViewHolder.endSaleTime_tv.setText(endTimeStr+"截止");
        childViewHolder.home_tv.setText(info.getHomeTeam());
        childViewHolder.away_tv.setText(info.getAwayTeam());

        childViewHolder.bqc_ss.setText("胜-胜\n"+info.getBqc_33());
        childViewHolder.bqc_sp.setText("胜-平\n"+info.getBqc_31());
        childViewHolder.bqc_sf.setText("胜-负\n"+info.getBqc_30());
        childViewHolder.bqc_ps.setText("平-胜\n"+info.getBqc_13());
        childViewHolder.bqc_pp.setText("平-平\n"+info.getBqc_11());
        childViewHolder.bqc_pf.setText("平-负\n"+info.getBqc_10());
        childViewHolder.bqc_fs.setText("负-胜\n"+info.getBqc_03());
        childViewHolder.bqc_fp.setText("负-平\n"+info.getBqc_01());
        childViewHolder.bqc_ff.setText("负-负\n"+info.getBqc_00());

        childViewHolder.bqc_ss.setChecked(info.isBqc_33);
        childViewHolder.bqc_sp.setChecked(info.isBqc_31);
        childViewHolder.bqc_sf.setChecked(info.isBqc_30);
        childViewHolder.bqc_ps.setChecked(info.isBqc_13);
        childViewHolder.bqc_pp.setChecked(info.isBqc_11);
        childViewHolder.bqc_pf.setChecked(info.isBqc_10);
        childViewHolder.bqc_fs.setChecked(info.isBqc_03);
        childViewHolder.bqc_fp.setChecked(info.isBqc_01);
        childViewHolder.bqc_ff.setChecked(info.isBqc_00);

        info.btnHGPojo.get(info.BQC_33).isSel = info.isBqc_33;
        info.btnHGPojo.get(info.BQC_31).isSel = info.isBqc_31;
        info.btnHGPojo.get(info.BQC_30).isSel = info.isBqc_30;
        info.btnHGPojo.get(info.BQC_13).isSel = info.isBqc_13;
        info.btnHGPojo.get(info.BQC_11).isSel = info.isBqc_11;
        info.btnHGPojo.get(info.BQC_10).isSel = info.isBqc_10;
        info.btnHGPojo.get(info.BQC_03).isSel = info.isBqc_03;
        info.btnHGPojo.get(info.BQC_01).isSel = info.isBqc_01;
        info.btnHGPojo.get(info.BQC_00).isSel = info.isBqc_00;

        info.btnHGPojo.put(info.BQC_33,info.btnHGPojo.get(info.BQC_33));
        info.btnHGPojo.put(info.BQC_31,info.btnHGPojo.get(info.BQC_31));
        info.btnHGPojo.put(info.BQC_30,info.btnHGPojo.get(info.BQC_30));
        info.btnHGPojo.put(info.BQC_13,info.btnHGPojo.get(info.BQC_13));
        info.btnHGPojo.put(info.BQC_11,info.btnHGPojo.get(info.BQC_11));
        info.btnHGPojo.put(info.BQC_10,info.btnHGPojo.get(info.BQC_10));
        info.btnHGPojo.put(info.BQC_03,info.btnHGPojo.get(info.BQC_03));
        info.btnHGPojo.put(info.BQC_01,info.btnHGPojo.get(info.BQC_01));
        info.btnHGPojo.put(info.BQC_00,info.btnHGPojo.get(info.BQC_00));
        Log.e("fjl----", JSON.toJSONString(info));
        jiSuanChangCi();

        childViewHolder.bqc_ss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_33 = !info.isBqc_33;
                if (info.isBqc_33) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_sp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_31 = !info.isBqc_31;
                if (info.isBqc_31) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_sf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_30 = !info.isBqc_30;
                if (info.isBqc_30) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_ps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_13 = !info.isBqc_13;
                if (info.isBqc_13) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_pp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_11 = !info.isBqc_11;
                if (info.isBqc_11) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_pf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_10 = !info.isBqc_10;
                if (info.isBqc_10) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_fs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_03 = !info.isBqc_03;
                if (info.isBqc_03) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_fp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_01 = !info.isBqc_01;
                if (info.isBqc_01) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.bqc_ff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isBqc_00 = !info.isBqc_00;
                if (info.isBqc_00) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        return convertView;
    }

    @Override
    protected void jiSuanChangCi() {
        changCi = 0;
        for (int i = 0; i < infos.size(); i++) {
            List<BeiDanChildBean> childBeens = infos.get(i).childBeen;
            for (int j = 0; j < childBeens.size(); j++) {
                if (childBeens.get(j).checkNum > 0) {
                    changCi++;
                }
            }
        }
        if (listener != null) {
            listener.onCheckBall(changCi);
        }
    }

    private class ChildViewHolder {
        TextView matchNum_tv;
        TextView matchName_tv;
        TextView endSaleTime_tv;
        TextView home_tv;
        TextView away_tv;
        CheckBox bqc_ss;
        CheckBox bqc_sp;
        CheckBox bqc_sf;
        CheckBox bqc_ps;
        CheckBox bqc_pp;
        CheckBox bqc_pf;
        CheckBox bqc_fs;
        CheckBox bqc_fp;
        CheckBox bqc_ff;
    }

}
